package cn.edu.zjgsu.ito.service;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.hssf.util.Region;

import cn.edu.zjgsu.ito.model.TeacherModel;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

public class AdminExportAcaInfService {
	public List<Record> CourseGrade() {
		try {
			
			List<Record> record=Db.find("select * from college");
			return record;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}
	
	
	public void exportExcel(List<Record> dataset, OutputStream out){
		System.out.println("+++++++++++++++++++++++++++++++++");
		 HSSFWorkbook workbook = new HSSFWorkbook();      
        HSSFSheet sheet = workbook.createSheet("new   sheet");      
        HSSFCellStyle style = workbook.createCellStyle(); // 样式对象 
        HSSFCellStyle style1 = workbook.createCellStyle(); // 样式对象
        //字体  加粗
        HSSFFont font1 = workbook.createFont();

	  	 font1.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
	  	 font1.setFontHeightInPoints((short) 12);//设置字体大小
	  	 font1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗体显示
	  	 
	  	//字体  加粗
        HSSFFont font = workbook.createFont();

	  	 font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
	  	 font.setFontHeightInPoints((short) 12);//设置字体大小
        
	  	    // 设置这些样式

	      style.setFillForegroundColor(HSSFColor.WHITE.index);

	      style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

	      style.setBorderBottom(HSSFCellStyle.BORDER_THIN);

	      style.setBorderLeft(HSSFCellStyle.BORDER_THIN);

	      style.setBorderRight(HSSFCellStyle.BORDER_THIN);

	      style.setBorderTop(HSSFCellStyle.BORDER_THIN);

	      style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
 
	      style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直  
      
	      style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平
	      		             
	      style.setFont(font1);
	      
	      
	      style1.setFillForegroundColor(HSSFColor.WHITE.index);

	      style1.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

	      style1.setBorderBottom(HSSFCellStyle.BORDER_THIN);

	      style1.setBorderLeft(HSSFCellStyle.BORDER_THIN);

	      style1.setBorderRight(HSSFCellStyle.BORDER_THIN);

	      style1.setBorderTop(HSSFCellStyle.BORDER_THIN);

	      style1.setAlignment(HSSFCellStyle.ALIGN_CENTER);
 
	      style1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直  
      
	      style1.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平
	      
	      style1.setFont(font);
	      style1.setWrapText(true);
          style.setWrapText(true);
      
	       int width=5000;
	       int height=500;
	       int height1=350;
	       
	       HSSFRow row3 = sheet.createRow((short) 0);
	       HSSFRow row;                
        
        sheet.createRow(0).setHeight((short) height1);//第四行
  
        //列宽
        sheet.setColumnWidth(0, 1000);
        for(int i=1;i<4;i++){
        	sheet.setColumnWidth(i, width);
        }
        
        Iterator<Record> it = dataset.iterator();
        //第四行
        String[] Headers = { "序号","学院名称", "学院编码"};
        for (short i = 0; i <Headers.length; i++) {

	         HSSFCell cell = row3.createCell(i+1);

	         cell.setCellStyle(style);

	         HSSFRichTextString text = new HSSFRichTextString(Headers[i]);

	         cell.setCellValue(text);

	      }
	      
        try{
       	 int index = 0,order=1;
  	      
  	      while (it.hasNext()) {
  	    	  index++;

  	          row = sheet.createRow(index);

  	          Record t = (Record) it.next();
  	          
  	          String[] Context = { String.valueOf(order++), t.getStr("collegeName"), t.getStr("collegeCode")};
  	          for (short i = 0; i < Headers.length; i++) {
  	 
  	 	         HSSFCell cell = row.createCell(i+1);

  	 	         cell.setCellStyle(style1);

  	 	         HSSFRichTextString text = new HSSFRichTextString(Context[i]);

  	 	         cell.setCellValue(text);

  	 	      }
	    	  
  	      }
       	 
        }catch(Exception e){
       	 e.printStackTrace();
        }
        
	      
   

	      try {

	         workbook.write(out);

	      } catch (IOException e) {

	         // TODO Auto-generated catch block

	         e.printStackTrace();

	      }
  
	}
}
